package com.example.project.activity.mingxi;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.android.volley.VolleyError;
import com.example.project.R;
import com.example.project.bean.AccountBean;
import com.example.project.db.DBManager;
import com.example.project.dialog.AlterDialog;
import com.example.project.dialog.BeiZhuDialog;
import com.example.project.volley.ConstantUtil;
import com.example.project.volley.Params;
import com.example.project.volley.VolleyInterface;
import com.example.project.volley.VolleyRequest;

import java.text.DecimalFormat;
//单个账单详情页面
public class MessageActivity extends AppCompatActivity implements View.OnClickListener {
    private TextView tvType,tvMoney,tvCommentTime,tvComment,tvXx,tvfx; //类型.金额,时间,备注,名称，分享
    private ImageView ivFh,ivTop; //返回箭头,图像
    private Button btXg,btSc; //修改,删除
    private String id;
    AccountBean accountBean; //将插入到记账本的数据保存到对象
    String type,money,time,beizhu,sImageId,kind,name; //类型，金额，时间，备注，图片，类型，名字

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_message );
        accountBean=new AccountBean(  );
        init();
    }

    //初始化
    private void init() {
        tvfx=findViewById( R.id.tv_fx );
        tvfx.setOnClickListener(this);
        tvType=findViewById( R.id.tv_type );
        tvMoney=findViewById( R.id.tv_money );
        tvCommentTime=findViewById( R.id.tv_comment_time );
        tvComment=findViewById( R.id.tv_comment );
        tvXx=findViewById( R.id.tv_xx );

        ivFh=findViewById( R.id.iv_fh );
        ivFh.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        } );
        ivTop=findViewById( R.id.iv_top );

        btXg=findViewById( R.id.bt_xg );
        btXg.setOnClickListener( this );
        btSc=findViewById( R.id.bt_sc );
        btSc.setOnClickListener( this );
        MsggetIntent();
    }

    //获取intent传过来的值
    private void MsggetIntent() {
        Intent intent=getIntent();
        type=intent.getStringExtra( "type" );
        money=intent.getStringExtra( "money" );
        time=intent.getStringExtra( "time" );
        beizhu=intent.getStringExtra( "beizhu" );
        sImageId=intent.getStringExtra( "sImageId" );
        kind=intent.getStringExtra( "kind" );
        name=null;
        if(kind.equals( "1" )){
            name="收入";
        }else if(kind.equals( "0" )){
            name="支出";
        }
        int ImageId=Integer.parseInt( sImageId );
        tvType.setText( name );
        tvMoney.setText( money );
        tvCommentTime.setText( time );
        tvComment.setText( beizhu );
        ivTop.setImageResource( ImageId );
        tvXx.setText( type  );
        id=intent.getStringExtra( "Id" );
        System.out.println( "id:"+id );
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.bt_xg:
                final AlterDialog dialog=new AlterDialog( MessageActivity.this );
                dialog.show(); //显示弹窗
                dialog.setDialogSize(); //动态设置弹窗大小
                dialog.setOnEnsureListener( new BeiZhuDialog.OnEnsureListener() {
                    @Override
                    public void onEnsure() {
                        String msg=dialog.getEditText(); //将备注的信息获取出来
                        if(TextUtils.isEmpty( msg )){ //如果为空，就还剩设置为空的
                            Toast.makeText( MessageActivity.this,"你输入的为空",Toast.LENGTH_SHORT ).show();

                        }else {
                            Float money=Float.parseFloat( msg );
                            DecimalFormat decimalFormat=new DecimalFormat(".00");//构造方法的字符格式这里如果小数不足2位,会以0补足.
                            String p=decimalFormat.format(money);//format 返回的是字符串
                            float moneys=Float.parseFloat(p);
                            accountBean.setMoney( moneys );
                            volleyUpdate(id,moneys+"");
                            Intent intent=new Intent( MessageActivity.this, MainActivity.class );
                            startActivity( intent );
                        }
                        dialog.cancel(); //废除对话框，才能进行activity销毁
                    }
                } );
                break;
            case R.id.bt_sc:
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("提示信息").setMessage("您确定要删除这条记录么？")
                        .setNegativeButton("取消",null)
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                //执行删除的操作
                                DBManager.deleteItemFromAccounttbById(Integer.parseInt(id));
                                volleyDelete(id);
                                Intent intent=new Intent( MessageActivity.this, MainActivity.class );
                                startActivity( intent );
                            }
                        });
                builder.create().show();   //显示对话框
                break;
            case R.id.tv_fx:
                Intent intent=new Intent( MessageActivity.this, ItemShareActivity.class );
                intent.putExtra( "time",time );
                intent.putExtra( "type",type);
                intent.putExtra( "name",name);
                intent.putExtra( "money",money);
                startActivity( intent );
                break;
        }
    }

    //删除主页的item信息
    public void volleyDelete(String id) {
        VolleyInterface vif=null;
        VolleyRequest.RequesetPost( ConstantUtil.DELECT, "DELECT", Params.Delect(id),new VolleyInterface( this ){
            @Override
            public void onMySuccess(String jsonStr) {
                    Toast.makeText( MessageActivity.this,"删除成功",Toast.LENGTH_SHORT ).show();
            }
            @Override
            public void onMyError(VolleyError error) {
                Toast.makeText(MessageActivity.this, "删除失败", Toast.LENGTH_LONG).show();
            }
            @Override
            public void onImageSuccess(Bitmap bitmap) {

            }
        });
    }

    //更新主页的item信息
    public void volleyUpdate(String id,String money) {
        VolleyRequest.RequesetPost( ConstantUtil.UPDATE, "Update", Params.Update(id,money),new VolleyInterface( this ){
            @Override
            public void onMySuccess(String jsonStr) {
                if(jsonStr.equals("1")){
                    Toast.makeText( MessageActivity.this,"更新成功",Toast.LENGTH_SHORT ).show();
                }else {
                    Toast.makeText( MessageActivity.this,"更新失败",Toast.LENGTH_SHORT ).show();
                }

            }
            @Override
            public void onMyError(VolleyError error) {
                Toast.makeText( MessageActivity.this,"更新失败",Toast.LENGTH_SHORT ).show();
            }
            @Override
            public void onImageSuccess(Bitmap bitmap) {

            }
        });
    }
}